<?php
/**
 * template.func.php
 */

if(!defined('IN_SITE')) {
	exit('Access Denied');
}

function template_compile($tplfile, $templateid, $tpldir)
{
	$data = @file_get_contents($tplfile);
	if($data === false) exit("$tplfile is not exists!");
	$file = basename($tplfile, '.htm');
	$objfile = SITE_ROOT."./data/templates/{$templateid}_$file.tpl.php";
	$data = template_parse($data);
	$strlen = file_put_contents($objfile, $data);
	@chmod($objfile, 0777);
	return $strlen;
}

function template_parse($data)
{
	$subtemplates = array();
	for($i = 1; $i<=3; $i++) {
		if(str_exists($data, '{subtemplate')) {
			$data = preg_replace("/\{subtemplate\s+([a-z0-9_]+)\}/ies", "loadsubtemplate('\\1')", $data);
		}
	}

	$data = preg_replace("/([\n\r]+)\t+/s", "\\1", $data);
	$data = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $data);
	$data = preg_replace("/\{template\s+(.+)\}/", "<?php include template(\\1); ?>", $data);
	$data = preg_replace("/\{include\s+(.+)\}/", "<?php include \\1; ?>", $data);
	$data = preg_replace("/\{php\s+(.+)\}/", "<?php \\1?>", $data);
	$data = preg_replace("/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $data);
	$data = preg_replace("/\{else\}/", "<?php } else { ?>", $data);
	$data = preg_replace("/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $data);
	$data = preg_replace("/\{\/if\}/", "<?php } ?>", $data);
	$data = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/", "<?php if(is_array(\\1)) foreach(\\1 as \\2) { ?>", $data);
	$data = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php if(is_array(\\1)) foreach(\\1 as \\2 => \\3) { ?>", $data);
	$data = preg_replace("/\{\/loop\}/", "<?php } ?>", $data);
	$data = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $data);
	$data = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>", $data);
	$data = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","<?php echo \\1;?>", $data);
	$data = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es", "addquote('<?php echo \\1;?>')", $data);
	$data = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>", $data);
	
	$data = preg_replace("/\"(http)?[\w\.\/:]+\?[^\"]+?&[^\"]+?\"/e", "transamp('\\0')", $data);
	$data = preg_replace("/\<script[^\>]*?src=\"(.+?)\"(.*?)\>\s*\<\/script\>/ise", "stripscriptamp('\\1', '\\2')", $data);

	$data = "<? if(!defined('IN_SITE')) exit('Access Denied');?>\r\n$data";
	return $data;
}

function loadsubtemplate($file, $tpldir)
{
	global $subtemplates;

	$tpldir = $tpldir ? $tpldir : TPLDIR;
	
	$tplfile = SITE_ROOT.'./templates/'.$tpldir.'/'.$file.'.htm';
	if(!file_exists($tplfile)) {
		$tplfile = SITE_ROOT.'./templates/default/'.$file.'.htm';
	}
	$data = @implode('', file($tplfile));
	$subtemplates[] = $tplfile;
	return $data;
}
	
function addquote($var)
{
	return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}

function transamp($str) {
	$str = str_replace('&', '&amp;', $str);
	$str = str_replace('&amp;amp;', '&amp;', $str);
	$str = str_replace('\"', '"', $str);
	return $str;
}

function stripscriptamp($str, $extra) {
	$extra = str_replace('\\"', '"', $extra);
	$str = str_replace('&amp;', '&', $str);
	return "<script src=\"$str\" type=\"text/javascript\"$extra></script>";
}
?>